<template>
  <div>
    <van-nav-bar title="V头条-登录" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        required
        placeholder="请输入11位手机号"
        :rules="[{ required: true, message: '请填写正确的手机号',pattern:/^1[3-9]\d{9}$/ }]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        label="密码"
        required
        placeholder="请输入6位密码"
        :rules="[{ required: true, message: '请填写密码',pattern:/^\d{6}$/ }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" :disabled="isLoading" :loading="isLoading" loading-text="正在登录中......"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginApi } from '@/api'
import Notify from '@/ui/Notify.js'
import { setToken } from '@/utils/token.js'
export default {
  data () {
    return {
      user: {
        mobile: '17352923044', // 手机号
        code: '246810'// 验证码（密码246810）
      },
      isLoading: false// 登录按钮加载状态
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      this.isLoading = true
      try {
        const res = await loginApi(this.user)
        console.log(res)
        Notify({ type: 'success', message: '登录成功了' })
        setToken(res.data.data.token)
        localStorage.setItem('refresh_token', res.data.data.refresh_token)
        this.$router.replace({
          path: this.$route.query.path || '/layout/home'
        })
      } catch (error) {
        Notify({ type: 'danger', message: '账号或密码错误' })
      }
      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar{
  background-color: #007bff;
  /deep/ .van-nav-bar__title{
    color: white;
  }
}
</style>
